package org.xs.web.http.servlet;

import java.util.Collection;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpSession;

public class ServletSessionMap implements Map<String, Object>{
	private HttpSession session;
	
	public ServletSessionMap(HttpSession session){
		this.session = session;
	}
	@Override
	public void clear() {
		 throw new UnsupportedOperationException(); 
	}

	@Override
	public boolean containsKey(Object key) {
		
		return session.getAttribute((String)key) != null;
	}

	@Override
	public boolean containsValue(Object value) {
		 throw new UnsupportedOperationException(); 
	}

	@Override
	public Set<java.util.Map.Entry<String, Object>> entrySet() {
		 throw new UnsupportedOperationException(); 
	}

	@Override
	public Object get(Object key) {
		return session.getAttribute((String)key);
	}

	@Override
	public boolean isEmpty() {
		return false;
	}

	@Override
	public Set<String> keySet() {
		HashSet<String> keys = new HashSet<String>();
		Enumeration<String> es = session.getAttributeNames();
		while(es.hasMoreElements()){
			keys.add(es.nextElement());
		}
		return keys;
	}

	@Override
	public Object put(String key, Object value) {
		Object result = session.getAttribute(key);
		session.setAttribute(key, value);
		return result;
	}

	@Override
	public void putAll(Map<? extends String, ? extends Object> m) {
		 throw new UnsupportedOperationException(); 
	}

	@Override
	public Object remove(Object key) {
		Object result = session.getAttribute((String)key);
		session.removeAttribute((String)key);
		return result;
	}

	@Override
	public int size() {
		 throw new UnsupportedOperationException(); 
	}

	@Override
	public Collection<Object> values() {
		 throw new UnsupportedOperationException(); 
	}

}
